package com.study.Util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @Author hupengming
 * @ClassName
 * @Date 2022-11-11 11:01
 * @Version 1.0
 */
public class HttpHelper {

    /**
     * 解析请求头信息
     *
     * @param request
     * @return
     */
    public static String getBodyString(HttpServletRequest request){
        StringBuffer streamStr = new StringBuffer();
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while((line = reader.readLine()) != null){
                streamStr.append(line);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return streamStr.toString();
    }
}
